--self = cast(Token,nil);
Token = {};
Token.__index = Token;

local function getstring(code,pos)
    local base = pos;
    pos = pos + 1;
    while(true)do
        local ps,pe,str = code:find('([\\"])',pos);
        if(str=='"')then
            pos = pe;
            break;
        end
        pos = pe + 2;
    end
    return code:sub(base,pos),pos;
end

function Token:Parse()
    --Token:tokens
    --Token:code
    local pos;
    local code = self.code;
    self.tokens = {};
    self.tokensPos = {};

    local function cmpansi(c,l,h)
        return (c>=l and c<=h);
    end
    while(true)do
        local ps,pe,c,c2 = code:find('%s*.',pos); --过滤空格
        if(pe==nil)then break; end;
        ps,pe,c,c2 = code:find('(.)(.?)',pe);
        local token;
        if(c==nil)then
            break;
        elseif(c=='/' and c2=='/')then
            --注释
            pos = code:find('[\n]',ps);
        elseif(c=='/' and c2=='*')then
            --多行注释
            ps,pos = code:find('%*/',ps);
        elseif(cmpansi(c,'a','z') or cmpansi(c,'A','Z') or c=='_' or c=='$' or cmpansi(c,'0','9'))then
            ps,pos,token = code:find('([%w_$]+)%s*',ps);
        elseif(c=='"')then
            token,pos = getstring(code,ps);
        else
            ps,pos,token = code:find('(.)',ps);
        end
        if(token)then
            table.insert(self.tokens,token);
            self.tokensPos[#self.tokens] = ps;
        end
        pos = pos + 1;
    end
end

function Token:next(pos)
    if(pos==nil)then pos = 1; end;
    self.cur_pos = pos;
    pos = pos + 1;
    return self.tokens[pos-1],pos;
end

function Token:Seterr(err)

    assert(nil,'错误坐标:'..tostring(self.cur_pos) ..'\n' .. tostring(err));
end

function Token.init(code)
    local self = {};
    self.code = code;
    setmetatable(self, Token);
    self:Parse();
    return self;
end

local tokens = Token.init[[
    class AA{
    int x; //12300123
    /*30123*/
    int value(){
        this.a = "1239124";
        this.x = 0x200;
    }
}
]];

local v = 20;
